package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.BankEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sxy on 2017/11/3.
 */

public class BankCardListAdapter extends BaseAdapter {
    private List<BankEntity> mList;
    private Context mContext;
    private OnDelClickListener mOnDelClick;

    public BankCardListAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<BankEntity> list) {
        mList = list;
    }

    public void setOnDelClick(OnDelClickListener onDelClick) {
        mOnDelClick = onDelClick;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public BankEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_bank_card_list, null);
            holder.rel_bg = (RelativeLayout) convertView.findViewById(R.id.rel_bg);
            holder.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_number1 = (TextView) convertView.findViewById(R.id.tv_number1);
            holder.tv_number2 = (TextView) convertView.findViewById(R.id.tv_number2);
            holder.tv_single = (TextView) convertView.findViewById(R.id.tv_single);
            holder.tv_day = (TextView) convertView.findViewById(R.id.tv_day);
            holder.iv_del = (ImageView) convertView.findViewById(R.id.iv_del);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        BankEntity entity = mList.get(position);
        holder.tv_name.setText(entity.getName());
        holder.tv_number1.setText("****");
        holder.tv_number2.setText(entity.getBankNumber().substring(entity.getBankNumber().length() - 4));
        double l = Double.parseDouble(entity.getDailyTradingLimit());
        BigDecimal b = new BigDecimal(l / 10000);
        holder.tv_day.setText(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        double l2 = Double.parseDouble(entity.getSingleTransactionLimit());
        BigDecimal b2 = new BigDecimal(l2 / 10000);
        holder.tv_single.setText(b2.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        switch (entity.getBankCode()) {
            case "GDB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_gdb));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_gdb);
                break;
            case "ABC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_abc));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_abc);
                break;
            case "ICBC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_icbc));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_icbc);
                break;
            case "CMBC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_cmbc));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_cmbc);
                break;
            case "CEB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_ceb));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_ceb);
                break;
            case "HXBANK":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_hxbank));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_hxbank);
                break;
            case "SPABANK":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_spabank));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_spabank);
                break;
            case "CIB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_cib));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_cib);
                break;
            case "SPDB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_spdb));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_spdb);
                break;
            case "CITIC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_citic));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_citic);
                break;
            case "COMM":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_comm));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_comm);
                break;
            case "CMB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_cmb));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_cmb);
                break;
            case "CCB":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_ccb));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_ccb);
                break;
            case "BOC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_boc));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_boc);
                break;
            case "PSBC":
                holder.iv_logo.setImageDrawable(mContext.getResources().getDrawable(R.drawable.icon_psbc));
                holder.rel_bg.setBackgroundResource(R.drawable.bg_psbc);
                break;
        }
        if (mOnDelClick == null || entity.getPreferred().equals("Y")) {
            holder.iv_del.setVisibility(View.GONE);
        } else {
            holder.iv_del.setVisibility(View.VISIBLE);
            holder.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnDelClick.OnDelClick(position);
                }
            });
        }
        return convertView;
    }

    public interface OnDelClickListener {
        void OnDelClick(int position);
    }

    class Holder {
        RelativeLayout rel_bg;
        ImageView iv_logo;
        TextView tv_name;
        TextView tv_number1;
        TextView tv_number2;
        TextView tv_single;
        TextView tv_day;
        ImageView iv_del;
    }
}
